package com.ztom.beans.factory.support.instantiate

import com.ztom.beans.factory.support.pojo.BeanDefinition
import net.sf.cglib.proxy.Enhancer
import net.sf.cglib.proxy.MethodInterceptor

/**
 * @author ZhangTao
 */
class CglibSubclassingInstantiationStrategy : InstantiationStrategy {

    override fun instantiate(beanDefinition: BeanDefinition): Any {
        println("使用策略 CglibSubclassingInstantiationStrategy 创建 bean")
        val enhancer = Enhancer()
        enhancer.setSuperclass(beanDefinition.beanClazz.java)
        enhancer.setCallback(MethodInterceptor { o, _, args, p ->
            p.invokeSuper(o, args)
        })
        return enhancer.create()
    }
}